package de.homelabs.jaxbsandbox;

import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import org.apache.xml.serialize.OutputFormat;

import de.homelabs.jaxbsandbox.domain.ContentElement;
import de.homelabs.jaxbsandbox.domain.PageElement;
import de.homelabs.jaxbsandbox.domain.PageList;
import de.homelabs.jaxbsandbox.domain.TemplateElement;
import de.homelabs.jaxbsandbox.domain.TemplateList;
import de.homelabs.modules.content.domain.HLPageContent;
import de.homelabs.modules.content.domain.HLPageElement;
import de.homelabs.modules.content.domain.HLPageList;
import de.homelabs.modules.content.domain.HLPageType;

public class JaxbSandbox {

	/**
	 * @param args
	 * @throws JAXBException 
	 * @throws IOException 
	 */
	public static void main(String[] args) throws JAXBException, IOException {
		exportPageList();
	}

	public static void exportPageList() throws JAXBException, IOException{
		HLPageList pageListRoot = new HLPageList();
		
		//content
		HLPageContent c1 = new HLPageContent();
		c1.setId(1);
		c1.setTemplateKey(null);
		c1.setTyp(HLPageType.TYPE_HTML);
		c1.setContent("<p>Home</p>");
		
		HLPageContent c2 = new HLPageContent();
		c2.setId(1);
		c2.setTemplateKey(null);
		c2.setTyp(HLPageType.TYPE_HTML);
		c2.setContent("<p>Home</p>");
		
		List<HLPageContent> contentList = new ArrayList<HLPageContent>();
		contentList.add(c1);
		contentList.add(c2);
		
		//page element
		HLPageElement e1 = new HLPageElement();
		e1.setActive(true);
		e1.setId(1);
		e1.setName("HomePage");
		e1.setPageUrl("/home.htm");
		e1.setContents(contentList);
		
		HLPageElement e2 = new HLPageElement();
		e2.setActive(true);
		e2.setId(1);
		e2.setName("HomePage");
		e2.setPageUrl("/home.htm");
		e2.setContents(contentList);
		
		List<HLPageElement> pages = new ArrayList<HLPageElement>();
		pages.add(e1);
		pages.add(e2);
		
		//pageRoot
		pageListRoot.setPages(pages);
		
		//test export
		// create JAXB context and instantiate marshaller
		JAXBContext context = JAXBContext.newInstance(new Class[]{HLPageList.class});
		
		Marshaller m = context.createMarshaller();
		m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
		
		OutputFormat format = new OutputFormat();
		//format.setLineSeparator(LineSeparator.Windows);
		//format.setIndenting(true);
		//format.setLineWidth(40);       
		format.setIndent(4);
		//format.setPreserveSpace(true);
		//format.setMethod("XML");
		
		CDataContentHandler cdHandler = new CDataContentHandler(new FileOutputStream("output.xml"), format);
		m.marshal(pageListRoot, System.out);
		m.marshal(pageListRoot, cdHandler.asContentHandler());
		
		System.out.println(cdHandler.toString());
		
		Writer w = null;
		try {
			w = new FileWriter("hlpages.xml");
			m.marshal(pageListRoot, w);
		} finally {
			try {
				w.close();
			} catch (Exception e) {
			}
		}
	}
	
	public static void exportTest() throws JAXBException, IOException{
		List<TemplateElement> templates = new ArrayList<TemplateElement>();
		List<PageElement> pages = new ArrayList<PageElement>();
		
		/********************
		 * Templates		*
		 ********************/
		TemplateElement a1 = new TemplateElement();
		a1.setId(1);
		a1.setName("default");
		a1.setTemplateKey("default.ftl");
		a1.setTemplateUrl("de/homelabs/spring/freemarker/templates/default.ftl");
		templates.add(a1);
		
		TemplateElement a2 = new TemplateElement();
		a2.setId(2);
		a2.setName("test");
		a2.setTemplateKey("test.ftl");
		a2.setTemplateUrl("de/homelabs/spring/freemarker/templates/test.ftl");
		templates.add(a2);
		
		TemplateList tl = new TemplateList();
		tl.setTemplates(templates);
		
		/****************
		 * Pages		*
		 ****************/
		ContentElement c1 = new ContentElement();
		c1.setId(1);
		c1.setName("");
		c1.setContent("<p><h1>Herzlich Willkommen!</h1></p>");
		
		ContentElement c2 = new ContentElement();
		c2.setId(2);
		c2.setName("");
		c2.setContent("<p><h1>Impressum</h1></p>");
		
		PageElement p1 = new PageElement();
		p1.setId(1);
		p1.setName("Homepage");
		p1.setPageUrl("/index.htm");
		p1.getContentElements().add(c1);
		
		PageElement p2 = new PageElement();
		p2.setId(2);
		p2.setName("Impressum");
		p2.setPageUrl("/impressum.htm");
		p2.getContentElements().add(c2);
		
		pages.add(p1);
		pages.add(p2);
		
		PageList pl = new PageList();
		pl.setPages(pages);
		
		/****************
		 * JAX Service	*
		 ****************/
		// create JAXB context and instantiate marshaller
		JAXBContext context = JAXBContext.newInstance(new Class[]{TemplateList.class, PageList.class});
		
		Marshaller m = context.createMarshaller();
		m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
		m.marshal(tl, System.out);

		//jaxb for pages
		//JAXBContext context2 = JAXBContext.newInstance(PageList.class);
		//Marshaller m2 = context2.createMarshaller();
		m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
		m.marshal(pl, System.out);
		
		Writer w = null;
		try {
			w = new FileWriter("hltemplates.xml");
			m.marshal(tl, w);
			w = new FileWriter("hlpages.xml");
			m.marshal(pl, w);
		} finally {
			try {
				w.close();
			} catch (Exception e) {
			}
		}

		/*******************
		 * test unmarshall *
		 *******************/
		Unmarshaller um = context.createUnmarshaller();
		TemplateList utl = (TemplateList) um.unmarshal(new FileReader("hltemplates.xml"));
		System.out.println(utl.getTemplates().size());
	}
}
